package by.dezhits.liltweets.content;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import by.dezhits.liltweets.R;
import by.dezhits.liltweets.content.contract.PostContract;
import by.dezhits.liltweets.db.DBHelper;

public class PostProvider extends ContentProvider {

	private DBHelper dbHelper;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		dbHelper.deletePosts(selection);
		getContext().getContentResolver().notifyChange(
				PostContract.PostColumns.CONTENT_URI, null);
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		String text = values.getAsString(getContext().getString(R.string.text));
		String status = values.getAsString(getContext().getString(R.string.status));
		dbHelper.addPost(text,status);
		getContext().getContentResolver().notifyChange(
				PostContract.PostColumns.CONTENT_URI, null);
		return null;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		dbHelper = new DBHelper(getContext(), null);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		Cursor cursor = dbHelper.getPosts(selection,selectionArgs,sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
